package com.laotou99.bookManage.controller;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.laotou99.bookManage.IDao.BookNameMapper;
import com.laotou99.bookManage.IDao.BorrowBookDetailMapper;
import com.laotou99.bookManage.IDao.BorrowBookHeaderMapper;
import com.laotou99.bookManage.IDao.custNameMapper;
import com.laotou99.bookManage.domain.BookName;
import com.laotou99.bookManage.domain.BorrowBookDetail;
import com.laotou99.bookManage.domain.BorrowBookHeader;
import com.laotou99.bookManage.domain.custName;

@Controller
@RequestMapping(value="/borrowbook")
public class BorrowBookController {
	
	private static final Log logger = LogFactory.getLog(BorrowBookController.class);

	@Resource
	private BorrowBookHeaderMapper borrowBookHeaderMapper = null;
	
	@Resource
	private custNameMapper custNameMapper1 = null;
	
	@Resource
	private BorrowBookDetailMapper borrowBookDetailMapper = null;
	
	@Resource
	private BookNameMapper bookNameMapper = null;
	
	/**
	 * 显示所有借书列表
	 * */
	@RequestMapping(value="/showAllBorrowBook")
	public String showAllBorrowBook(Model model){
		logger.info("showAllBorrowBook");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<BorrowBookHeader> bbh1 = borrowBookHeaderMapper.selectAll();
		//for(BorrowBookHeader bbh2:bbh1){}
		model.addAttribute("borrowbooks", bbh1);
		return "showAllBorrowBooks";
	}
	
	/**
	 * 增加借书单
	 * */
	@RequestMapping(value="/addBorrowBook")
	public String addBorrowBook(Model model)
	{
		logger.info("addBorrowBook");
		List<custName> cn1 = custNameMapper1.selectAllcustName();
		model.addAttribute("custName1", cn1);
		model.addAttribute("borrowbookorder", new BorrowBookHeader());
		return "addBorrowBookOrder";
	}
	
	/**
	 * 保存借书明细数据
	 * */
	@RequestMapping(value="/saveBorrowDetail")
	public String saveBorrowDetail(
			@ModelAttribute BorrowBookDetail borrowBookDetail)
	{
		int num1 = borrowBookDetail.getNum();
		String strName = borrowBookDetail.getBookname();
		BookName bid1 =  bookNameMapper.selectByName(strName);
		
		bookNameMapper.updateByidDivNum(bid1.getId(), num1);
		borrowBookDetailMapper.insertSelective(borrowBookDetail);
		return "redirect:/borrowbook/showAllBorrowBook";
	}
	
	/**
	 * 确认还书
	 * */
	@RequestMapping(value="/updateDataBorrowOrder/{id}")
	public String updateDataBorrowOrder(@PathVariable int id)
	{
		logger.info("id->"+id);
		BorrowBookHeader bbh1 = borrowBookHeaderMapper.selectAllByPrimaryKey(id);
		logger.info("bbh1==null->");		logger.info(bbh1==null);		
		if(bbh1==null){				}else{
			List<BorrowBookDetail> bbd1 =bbh1.getBorrowBookDetail();
			for(BorrowBookDetail bbd2:bbd1){
				logger.info("bookname:"+bbd2.getBookname());
				logger.info("Num:"+bbd2.getNum());
				try {
					BookName bid1 =  bookNameMapper.selectByName(bbd2.getBookname());
					int i = bookNameMapper.updateByidAddNum(bid1.getId(), bbd2.getNum());
				} catch (Exception e) { 
					e.printStackTrace();
				}
			}
		}	
		bookNameMapper.updateByIsReturn(id);
		return "redirect:/borrowbook/showAllBorrowBook";
	}
	
	/**
	 * 查看借书单明细
	 * */
	@RequestMapping(value="/showDetailBorrowOrder/{id}")
	public String showDetailBorrowOrder(Model model,@PathVariable int id)
	{
		logger.info("showDetailBorrowOrder");
		//BorrowBookHeader bbh1 = borrowBookHeaderMapper.selectAllByPrimaryKey(id);
		BorrowBookHeader bbh1 = borrowBookHeaderMapper.selectByPrimaryKey(id);
		//System.out.println(bbh1.checkDetail());
		System.out.println("bbh1==null->"+bbh1==null);
		logger.info("bbh1==null->");
		logger.info(bbh1==null);
		
		if(bbh1==null){
			bbh1 = borrowBookHeaderMapper.selectByPrimaryKey(id);
			model.addAttribute("borrowOrderDetail", bbh1);
		}else{
			model.addAttribute("borrowOrderDetail", bbh1);
			int i1 = bbh1.getId();
			//List<BorrowBookDetail> bbd1 = bbh1.getBorrowBookDetail();
			List<BorrowBookDetail> bbd1 = borrowBookDetailMapper.selectByIDKey(i1);
			logger.info("bbh1.getRemark1()->"+bbh1.getRemark1());
			logger.info("bbh1.getRemark2()->"+bbh1.getRemark2());
			//logger.info("bbh1.getHremark1()-> "+bbh1.getHremark1());
			//logger.info("bbh1.getHremark2()-> "+bbh1.getHremark2());
			for(BorrowBookDetail bbd2:bbd1){
				logger.info("bbd2.getRemark1()->"+bbd2.getRemark1());
				logger.info("bbd2.getRemark2()->"+bbd2.getRemark2());
			}
			model.addAttribute("borrowOrderDetail1", bbd1);
		}
		return "showDetailBorrowOrder1";
	}	
	
	/**
	 * 添加借书单明细
	 * */
	@RequestMapping(value="/addborrowBookDetail/{id}")
	public String addBorrowBookDetail(Model model,@PathVariable int id)
	{		
		List<BookName> bn1 = bookNameMapper.getAllBooks();
		model.addAttribute("bookName1", bn1);
		model.addAttribute("Hid", id);
		model.addAttribute("borrowBookDetail", new BorrowBookDetail());
		return "addBorrowBookDetail";
	}
	
	/**
	 * 保存新添加的借书单
	 * */
	@RequestMapping(value="/saveBorrowOrder")
	public String saveBorrowOrder(
			@ModelAttribute BorrowBookHeader borrowBookHeader)
	{//System.out.println(borrowBookHeader==null);
		borrowBookHeaderMapper.insertSelective(borrowBookHeader);
		List<BorrowBookHeader> bbh1 = borrowBookHeaderMapper.selectAll();
		//System.out.println(bbh1.get(0).getId());
		int i1 = bbh1.get(0).getId();
		//return "redirect:/borrowbook/addborrowBookDetail";
		return "redirect:/borrowbook/addborrowBookDetail/"+i1;
	}

}
